package com.itholmes.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.itholmes.pojo.Emp;
import com.itholmes.pojo.Result;
import com.itholmes.service.EmpService;
import com.itholmes.service.EmpServiceImpl;
import com.itholmes.utils.CookieUtils;
import com.itholmes.utils.ProxyFactory;
import com.itholmes.utils.ServletForJsonUtil;

@WebServlet("/empOneAdd")
public class EmpOneAdd extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		int user_id = CookieUtils.cookiesEaxm(req);
		if(user_id == -1) {
			resp.getWriter().write("登录失效或者已经过期，请重新回到登录页面进行登录。");
		}
		
		Emp emp = ServletForJsonUtil.jsonToObj(req, Emp.class);
		
		//代理类调用方法
		EmpService proxy = (EmpService)ProxyFactory.getProxy(EmpServiceImpl.class);
		int i = proxy.empAdd(emp, user_id);
		
		
		Result result = new Result();
		
		if(i>0) {
			result.setCode(1);
			result.setComment("添加成功");
		}else {
			result.setCode(0);
			result.setComment("添加失败");
		}
		
		Gson gson = new Gson();
		String str = gson.toJson(result);
		
		resp.getWriter().write(str);
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}
}
